<template>
  <div class="industry-home">
    <div class="header-container">
      <img @click="goBack" class="back-arrow" :src="backHome" alt="" />
      <div class="header-title">
        <span>产企图谱</span>
      </div>
    </div>
    <div class="search-list">
      <div
        class="search-item"
        :class="activeIndex == index ? 'active-item' : ''"
        @click="changeActiveTab(item, index)"
        v-for="(item, index) in searchList"
        :key="item.id"
      >
        <div class="item-content">
          <span class="search-item-title"> {{ item.title }} </span>
          <span class="search-item-content">
            {{ item.descInfo }}
          </span>
          <img :src="item.iconImg" class="search-item-icon" alt="" />
        </div>
      </div>
    </div>
    <div class="container">
      <keep-alive>
        <component :is="activeComponent"></component>
      </keep-alive>
    </div>
  </div>
</template>

<script setup>
import { ref, defineAsyncComponent, shallowRef, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { onBackPressed } from '@/utils/androidFn.js';
import { openConsole } from '@/utils/industryUtils.js';

import backHome from '@/assets/images/home.png';
import noBorderYellowStar from '@/assets/images/noBorderYellowStar.png';
import hotIcon from '@/assets/images/industryMap/hotIcon.png';
import majorIcon from '@/assets/images/industryMap/majorIcon.png';
import industryIcon from '@/assets/images/industryMap/industryIcon.png';
import collect from '@/assets/images/industryMap/collect.png';

const HotIndustry = defineAsyncComponent(() =>
  import('@/views/industryMap/industryHome/components/HotIndustry.vue')
);
const MajorSearchIndustry = defineAsyncComponent(() =>
  import('@/views/industryMap/industryHome/components/MajorSearchIndustry.vue')
);
const IndustryList = defineAsyncComponent(() =>
  import('@/views/industryMap/industryHome/components/IndustryList.vue')
);
const CollectList = defineAsyncComponent(() =>
  import('@/views/industryMap/industryHome/components/CollectList.vue')
);
defineOptions({
  name: 'IndustryDetail',
  // 判断来源路径,其他页面的路径清空,保持首页tab默认第一个
  beforeRouteEnter(to, from, next) {
    const fromName = from.name ? from.name.toLowerCase() : '';
    if (fromName !== 'industrydetail') {
      localStorage.removeItem('homeTabIndex');
    }
    next();
  },
});
openConsole(); //是否开启调试工具
const router = useRouter();

const searchList = [
  {
    title: '热门产业',
    descInfo: '了解热门产业，洞察发展趋势，发现更多专业选择方向',
    component: HotIndustry,
    iconImg: hotIcon,
  },
  {
    title: '按专业查询',
    descInfo: '输入专业，查询适配的战略新兴产业及国标行业方向',
    component: MajorSearchIndustry,
    iconImg: majorIcon,
  },
  {
    title: '按产业/行业查询',
    descInfo: '选择产业类别，查询产业介绍、发展趋势和代表性企业，了解对口专业',
    component: IndustryList,
    iconImg: industryIcon,
  },
  {
    title: '我的收藏',
    descInfo: '快速查看自己收藏关注的产业',
    component: CollectList,
    iconImg: collect,
  },
];
const activeIndex = ref(0);
const activeComponent = shallowRef(HotIndustry);
const changeActiveTab = (item, index) => {
  activeIndex.value = index;
  activeComponent.value = item.component;
  localStorage.setItem('homeTabIndex', index);
};

const goBack = () => {
  onBackPressed(router);
};

onMounted(() => {
  activeIndex.value = Number(localStorage.getItem('homeTabIndex')) || 0;
  activeComponent.value = searchList[activeIndex.value].component;
});
</script>
<style lang="scss" scoped>
div {
  box-sizing: border-box;
}
.industry-home {
  height: 100vh;
  background: #dff0ff;
  display: flex;
  flex-direction: column;
  font-family: PingFangSC, PingFang SC;
}
.header-container {
  margin-top: 26px;
  width: 100%;
  padding: 0px 60px;
  height: 88px;
  display: flex;
  align-items: center;
  .back-arrow {
    width: 48px;
    height: 48px;
  }
  .header-title {
    padding-left: 24px;
    padding-right: 72px;
    text-align: center;
    width: 100%;
    font-weight: bold;
    font-size: 36px;
    color: #000000;
  }
}
.search-list {
  padding: 28px 60px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-gap: 16px;
  .search-item {
    padding-bottom: 20px;
    position: relative;
    .item-content {
      height: 100%;
      display: flex;
      flex-direction: column;
      padding: 28px 28px 38px;
      box-shadow: 0px 2px 25px 0px rgba(75, 122, 192, 0.14);
      border-radius: 20px;
      position: relative;
    }
    .search-item-title {
      font-weight: bold;
      font-size: 28px;
      color: #0b6eff;
      line-height: 27px;
    }
    .search-item-content {
      margin-top: 18px;
      font-weight: 400;
      font-size: 18px;
      color: #797979;
      line-height: 28px;
    }
    .search-item-icon {
      position: absolute;
      height: 64px;
      bottom: 0;
      right: 8px;
      border-radius: 20px;
    }
    &.active-item::after {
      position: absolute;
      content: '';
      bottom: 4px;
      left: 50%;
      transform: translateX(-50%);
      border-top: 20px solid #ffffff;
      border-right: 14px solid transparent;
      border-left: 14px solid transparent;
    }
    &:nth-child(1) {
      .item-content {
        background: url('@/assets/images/industryMap/bgBlue.png') no-repeat;
        background-size: 100% 100%;
      }
      &.active-item {
        background: url('@/assets/images/industryMap/bgBlueActive.png')
          no-repeat;
        background-size: 100% 100%;
        .item-content {
          background: none;
        }
      }
      .search-item-title {
        color: #0b6eff;
      }
      .search-item-icon {
        height: 64px;
      }
    }
    &:nth-child(2) {
      .item-content {
        background: url('@/assets/images/industryMap/bgPink.png') no-repeat;
        background-size: 100% 100%;
      }
      &.active-item {
        background: url('@/assets/images/industryMap/bgPinkActive.png')
          no-repeat;
        background-size: 100% 100%;
        .item-content {
          background: none;
        }
      }
      .search-item-title {
        color: #8b56db;
      }
      .search-item-icon {
        height: 52px;
      }
    }
    &:nth-child(3) {
      .item-content {
        background: url('@/assets/images/industryMap/bgGreen.png') no-repeat;
        background-size: 100% 100%;
      }
      &.active-item {
        background: url('@/assets/images/industryMap/bgGreenActive.png')
          no-repeat;
        background-size: 100% 100%;
        .item-content {
          background: none;
        }
      }
      .search-item-title {
        color: #24a799;
      }
      .search-item-icon {
        height: 60px;
      }
    }
    &:nth-child(4) {
      .item-content {
        background: url('@/assets/images/industryMap/bgOrange.png') no-repeat;
        background-size: 100% 100%;
      }
      &.active-item {
        background: url('@/assets/images/industryMap/bgOrangeActive.png')
          no-repeat;
        background-size: 100% 100%;
        .item-content {
          background: none;
        }
      }
      .search-item-title {
        color: #df8001;
      }
      .search-item-icon {
        height: 68px;
      }
    }
  }
}
.container {
  width: 100%;
  flex: 1;
  overflow: hidden;
}
</style>
